package com.example.jdai.jdai;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.example.jdai.jdai.config.Config;
import com.example.jdai.jdai.rest.BackRest;
import com.example.jdai.jdai.rest.RestApi;
import com.example.jdai.jdai.ui.ConnectActivity;
import com.example.jdai.jdai.ui.LnfoActivity;
import com.example.jdai.jdai.ui.MainActivity;
import com.example.jdai.jdai.ui.PhoneActivity;
import com.example.jdai.jdai.ui.TbActivity;
import com.example.jdai.jdai.ui.ZmWebActivity;
import com.example.jdai.jdai.util.DataAll;
import com.example.jdai.jdai.util.DataConnect;
import com.example.jdai.jdai.util.DataLnfo;
import com.example.jdai.jdai.util.DataPhone;
import com.example.jdai.jdai.util.DataZfb;
import com.example.jdai.jdai.util.Tools;
import com.google.gson.Gson;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;

/**
 * Created by hellopyj on 2017/wel_01/5.
 */

public class MyApplication extends Application {
    public  static Context applicationContext;
   // public  static Retrofit retrofit;
    public  static SharedPreferences sp;
    public static LocationClient location;
    public static DataAll dataAll;
    public static BackRest backRest;
    public static OkHttpClient okClient;
    public static Gson gson ;
    public static Activity main;
    public static Class[] acList={LnfoActivity.class, ConnectActivity.class, ZmWebActivity.class,
            TbActivity.class, PhoneActivity.class
    };

    @Override
    public void onCreate() {
        applicationContext=getApplicationContext();
        sp= applicationContext.getSharedPreferences(Config.SP_NAME,Context.MODE_PRIVATE);
        //初始化rest
//        retrofit = new Retrofit.Builder()
//                .baseUrl(Config.BASE_URL)
//                .addConverterFactory(GsonConverterFactory.create())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                .build();
        try {
            Cipher.getInstance("AES/CBC/PKCS5PADDING");
            SecureRandom.getInstance("SHA1PRNG");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
        dataAll=new DataAll();
        backRest=new BackRest();
        okClient= new OkHttpClient().newBuilder().readTimeout(5, TimeUnit.MINUTES).build();
        gson=new Gson();
        if(Config.ISDEBUG)
        JPushInterface.setDebugMode(true); 	// 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);
        JPushInterface.setAlias(applicationContext, Tools.getM5(MyApplication.dataAll.data.get(MyApplication.dataAll.LOGIN_ID)), new TagAliasCallback() {
            @Override
            public void gotResult(int i, String s, Set<String> set) {
                if(Config.ISDEBUG)
                    Log.v("aria",s);
            }
        });

        //地图
        initLoc();
        super.onCreate();
//        if (LeakCanary.isInAnalyzerProcess(this)) {
//            // This process is dedicated to LeakCanary for heap analysis.
//            // You should not init your app in this process.
//            return;
//        }
//        LeakCanary.install(this);

    }





    private static Class getNext(Class tmp){

        if(tmp.equals(LnfoActivity.class)) {
            if(DataAll.ifOk(DataAll.dataConnect.data.get(DataConnect.CT_STATUS)))
                return getNext(acList[1]);
            return ConnectActivity.class;

        }
        if(tmp.equals(ConnectActivity.class)){
            if(DataAll.ifOk(MyApplication.dataAll.data.get(DataAll.ZIMA_STATUS)))
                return getNext(acList[2]);
            return ZmWebActivity.class;

        }
        if(tmp.equals(ZmWebActivity.class)){
            if(DataAll.ifOk(DataAll.dataZfb.data.get(DataZfb.ZFB_STATUS)))
                return getNext(acList[3]);
            return TbActivity.class;

        }
        if(tmp.equals(TbActivity.class)){
            if(DataAll.ifOk(DataAll.dataPhone.data.get(DataPhone.PHONE_CODE)))
                return getNext(acList[4]);
            return PhoneActivity.class;

        }
        if(tmp.equals(PhoneActivity.class)){
            return null;

        }
        return null;

    }
    public  static void startNext(Activity fir){
        Class next=getNext(fir.getClass());
        if(next==null) {
            return;
        }
        else
        fir.startActivity(new Intent(fir, getNext(fir.getClass())));
    }
    public void initLoc(){
        location=new LocationClient(applicationContext);
        LocationClientOption mOption = new LocationClientOption();
        mOption.setPriority(LocationClientOption.NetWorkFirst);
        mOption.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        mOption.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系，如果配合百度地图使用，建议设置为bd09ll;
        mOption.setScanSpan(0);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        mOption.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        mOption.setOpenGps(false);
        mOption.setIsNeedLocationDescribe(true);//可选，设置是否需要地址描述
        mOption.setNeedDeviceDirect(false);//可选，设置是否需要设备方向结果
        mOption.setLocationNotify(false);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        mOption.setIgnoreKillProcess(true);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        mOption.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        mOption.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        mOption.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        mOption.setIsNeedAltitude(false);//可选，默认false，设置定位时是否需要海拔信息，默认不需要，除基础定位版本都可用
        location.setLocOption(mOption);
        BDLocationListener dbList=new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {
                backRest.addLocal(bdLocation.getLongitude()+"",bdLocation.getLatitude()+"");
                //Toast.makeText(applicationContext,""+bdLocation.getLatitude(),Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onConnectHotSpotMessage(String s, int i) {

            }
        };
        location.registerLocationListener(dbList);

    }


}
